﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CallCenter.DAL;
using Maticsoft.Model;
using Microsoft.International.Converters.PinYinConverter;

namespace CallCenter.BLL
{
    partial class CustomerBLL
    {
        //CustomersDAL dal = new CustomersDAL();
        public bool Check(string carNum)
        {
            CustomersDAL dal = new CustomersDAL();
            CustomersModel model = dal.GetByCarNum(carNum);
            if (model != null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public void CreatePinyin()
        {
            CustomersModel[] customers = ListAll();
            foreach (CustomersModel cust in customers)
            {
                cust.NamePinyin = GetPinYin(cust.Name);
                dal.Update(cust);
            }

        }

        public CustomersModel[] GetByPinyin(string pinyin)
        {
            return dal.GetByPinYin(pinyin);
        }

        static string GetPinYin(string str)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char c in str)
            {
                if (!ChineseChar.IsValidChar(c))
                {
                    continue;
                }
                ChineseChar ch = new ChineseChar(c);
                string i = ch.Pinyins[0];
                sb.Append(i.Substring(0,i.Length-1));
            }
            return sb.ToString();
        }
    }
}
